exports.add = (req,res) => {

    //获取参数信息
    var bodyParams = req.routeInfo.bodyParams;

    //新增成功的节点ID
    var osid = 0;

    //新增用户ID
    var userid = 0;

    //新增组织结构信息
    function addOrganization(cb){

        //节点路径
        var os_path = "";
        if(bodyParams.u_type == 0){
            os_path = "0," + bodyParams.os_id + ",";
        }else{
            os_path = "0,1," + bodyParams.os_id + ",";
        }

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.organizationalStructureAdd.Request_Api_Url,
                method : 'POST',
                data   : {
                    os_name        : bodyParams.os_name,
                    os_path        : os_path,
                    os_fid         : bodyParams.os_id,
                    os_is_inherit  : 1,
                    os_child_fid   : bodyParams.os_child_fid,
                    os_master_type : bodyParams.os_master_type
                }
            },
            function (data) {

                osid = data.result;

                cb();
                
            }
        )

    }

    //更新节点信息表中的非自建节点路径
    function updateOrganizationPath(cb){

        //子节点路径
        var os_child_path = "";

        //子节点类型
        var os_child_type = 0;

        if(bodyParams.u_type == 1){
           os_child_path = bodyParams.os_id + "," + osid + ",";
           os_child_type = 1;
        }else{
           os_child_path = osid + ",";
        }

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.organizationalStructureUpdate.Request_Api_Url,
                method : 'POST',
                data   : {
                    update_where   : "os_id",
                    os_id          : osid,
                    os_child_path  : os_child_path,
                    os_child_type  : os_child_type
                }
            },
            function (data) {

                cb();
                
            }
        )

    }

    //新增节点扩展信息
    function addOrganizationExtensionInfo(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artOrganizationStructureExtensionInfoAdd.Request_Api_Url,
                method : 'POST',
                data   : {
                    osei_contact                          : bodyParams.osei_contact,
                    osei_phone                            : bodyParams.osei_phone,
                    osei_address                          : bodyParams.osei_address,
                    osei_create_time                      : common.nowTime(false),
                    osei_osid                             : osid,
                    osei_strategic_cooperation_percentage : bodyParams.osei_strategic_cooperation_percentage,
                    osei_agent_percentage                 : bodyParams.osei_agent_percentage,
                    osei_provinces                        : bodyParams.pid,
                    osei_city                             : bodyParams.cid,
                    osei_area                             : bodyParams.aid
                }
            },
            function (data) {

                cb();
                
            }
        )
    }

    //创建管理员账户
    function createManagerUser(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userAdd.Request_Api_Url,
                method : 'POST',
                data   : {
                    u_username      : bodyParams.u_username,
                    u_password      : bodyParams.u_password,
                    u_nickname      : bodyParams.u_nickname,
                    u_phone         : bodyParams.u_phone,
                    u_create_time   : common.nowTime(false),
                    u_is_activation : 1,
                    u_type          : bodyParams.os_master_type,
                    u_master_osid   : osid
                }
            },
            function (data) {

                userid = data.result;

                cb();
            }
        )
    }

    //将管理员添加进该组织结构节点
    function userToNode(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userOrganizationalMiddleAdd.Request_Api_Url,
                method : 'POST',
                data   : {
                    uom_osid : osid,
                    uom_uid  : userid
                }
            },
            function (data) {
                cb();
            }
        )
    }

    //将角色分配给新创建的节点
    function roleToNode(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.organizationalRoleMiddleAdd.Request_Api_Url,
                method : 'POST',
                data   : {
                    orm_osid  : osid,
                    orm_orid  : bodyParams.os_master_type == 1 ? 32 : 33
                }
            },
            function (data) {
                cb();
            }
        )
    }

    F.Serial([addOrganization,updateOrganizationPath,addOrganizationExtensionInfo,createManagerUser,userToNode,roleToNode],() => {
        RES.successResponse(res);
    })

}